﻿var VmTop = new Vue({
    el: "#divTop",
    data: {
        pwd: {
            showDialog: false,
            form: {
                pwd: "",
                newPwd: "",
                newPwd2: ""
            }
        },
        rules: {
            pwd: [
                { required: true, message: '请输入旧密码', trigger: 'blur' },
                { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
            ],
            newPwd: [
                { required: true, message: '请输入新密码', trigger: 'blur' },
                { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
            ],
            newPwd2: [
                { required: true, message: '请输入确认密码', trigger: 'blur' },
                { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
            ],
            lgnName: [
                { required: true, message: '请输入账号', trigger: 'blur' },
                { min: 4, max: 20, message: '长度在 4 到 20 个字符', trigger: 'blur' }
            ],
            lgnPwd: [
                { required: true, message: '请输入密码', trigger: 'blur' },
                { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
            ],
            code: [
                { required: true, message: '请输入校验码', trigger: 'blur' }
            ]
        },
        navOn: 0,
        login: {
            form: {
                lgnName: "",
                lgnPwd: "",
                code: ""
            },
            imgCodeUrl: "/image/GetCheckCode",
            show: false,
        }
    },
    mounted: function () {

    },
    methods: {
        logoff: function () {
            VmYz.confirm("确认退出系统吗？", function () { location.href = "/account/logoff"; });
        },
        showPwd: function () {
            if (this.$refs["pwd.form"] != undefined) {
                this.$refs["pwd.form"].resetFields();
            }
            this.pwd = {
                showDialog: true,
                form: {
                    pwd: "",
                    newPwd: "",
                    newPwd2: ""
                }
            }
        },
        SavePwd: function (formName) {
            var that = this;
            this.$refs[formName].validate(function (valid) {
                if (valid) {
                    if (that.pwd.form.newPwd != that.pwd.form.newPwd2) {
                        VmYz.message("两次密码不一致！");
                        return;
                    }
                    $.post("/Home/UpdatePwd", that.pwd.form, function (data) {
                        VmYz.message(data.msg, data.suc);
                        if (data.suc) {
                            that.pwd.showDialog = false;
                        }
                    });
                }
            });
        },
        showLogin: function () {
            this.login = {
                form: {
                    lgnName: "",
                    lgnPwd: "",
                    code: ""
                },
                imgCodeUrl: "/image/GetCheckCode?d=" + new Date().getTime(),
                show: true,
            };
        },
        getImgCode: function () {
            this.login.imgCodeUrl = "/image/GetCheckCode?d=" + new Date().getTime();
        },
        loginUser: function (formName) {
            var that = this;
            this.$refs[formName].validate(function (valid) {
                if (valid) {
                    $.post("/account/LoginUser", that.login.form, function (data) {
                        VmYz.message(data.msg, data.suc);
                        if (data.suc) {
                            if (data.url == "") {
                                location.href = location.href;
                            }
                            else { 
                                location.href = data.url;
                            }
                        }
                        else {
                            that.getImgCode();
                        }
                    });
                }
            });
        }
    }
})